package com.zhb.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhb.lkd.base.PageResponse;
import com.zhb.lkd.exception.BusinessException;
import com.zhb.lkd.mapper.VendingMachineMapper;
import com.zhb.lkd.pojos.entity.Partner;
import com.zhb.lkd.mapper.PartnerMapper;
import com.zhb.lkd.pojos.entity.Sku;
import com.zhb.lkd.pojos.entity.SkuClass;
import com.zhb.lkd.pojos.entity.VendingMachine;
import com.zhb.lkd.pojos.vo.PartnerVo;
import com.zhb.lkd.pojos.vo.SkuVo;
import com.zhb.lkd.service.IPartnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 合作商表 服务实现类
 * </p>
 *
 * @author haibin
 * @since 2024-08-18
 */
@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, Partner> implements IPartnerService {
    @Resource
    private VendingMachineMapper vendingMachineMapper;
    /**
     * 合作商搜索
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageResponse search(Integer pageIndex, Integer pageSize, String name) {
        //1.检查参数
        //2.分页查询
        Page<Partner> page = lambdaQuery()
                .like(StrUtil.isNotBlank(name), Partner::getName, name)
                .page(new Page<>(pageIndex, pageSize));
        List<PartnerVo> partnerVos = page.getRecords().stream().map(partner -> {
            PartnerVo partnerVo = BeanUtil.copyProperties(partner, PartnerVo.class);
            //查询设备数量
            Long count= vendingMachineMapper.selectCount(Wrappers.<VendingMachine>lambdaQuery().eq(VendingMachine::getOwnerId, partner.getId()));
            partnerVo.setVmCount(Math.toIntExact(count));
            return partnerVo;
        }).collect(Collectors.toList());
        return new PageResponse(page,partnerVos);
    }

    /**
     * 新增合作商
     * @param partner
     */
    @Override
    public void create(Partner partner) {
        //1.检查参数
        if (StrUtil.isBlank(partner.getName())){
            throw new RuntimeException("合作商名称不能为空");
        }
        //2.保存
        save(partner);
    }

    @Override
    public void deleteById(String id) {
        //1.检查参数
        if (StrUtil.isBlank(id)){
            throw new RuntimeException("id不能为空");
        }
        //2.删除
        removeById(id);
    }

    /**
     * 获取合作商详情
     * @param id
     * @return
     */
    @Override
    public PartnerVo findById(String id) {
        //1.检查参数
        if (StrUtil.isBlank(id)){
            throw new BusinessException(500,"id不能为空");
        }
        //判断id必须是数字
        if (!StrUtil.isNumeric(id)){
            throw new BusinessException(500,"id必须是数字");
        }
        //2.查询
        Partner partner = getById(id);
        PartnerVo partnerVo = BeanUtil.copyProperties(partner, PartnerVo.class);
        Long count= vendingMachineMapper.selectCount(Wrappers.<VendingMachine>lambdaQuery().eq(VendingMachine::getOwnerId, partner.getId()));
        if (count != null){
            partnerVo.setVmCount(0);
        }else {
            partnerVo.setVmCount(Math.toIntExact(count));
        }
        return partnerVo;
    }

    @Override
    public void resetPwd(String id) {
        //1.检查参数
        if (StrUtil.isBlank(id)){
            throw new BusinessException(500,"id不能为空");
        }
        //2.重置密码
        Partner partner = new Partner();
        partner.setPassword("123456");
        partner.setId(Integer.valueOf(id));
        updateById(partner);
    }

    /**
     * 合作商数量
     * @return
     */
    @Override
    public Long partnerCount() {
        long count = count();
        return count;
    }
}
